﻿using System;
using System.Collections.Generic;

using System.Text;

namespace RoughJs.Error
{
    public class EvaluatorException : RhinoException
    {
        public EvaluatorException(String msg) : base(msg) { }
        public EvaluatorException(String msg, String sourceName,
                              int lineNumber)
            : this(msg, sourceName, lineNumber, null, 0)
        {

        }
        public EvaluatorException(String msg, String sourceName, int lineNumber,
                              String lineSource, int columnNumber)
            : base(msg)
        {

            recordErrorOrigin(sourceName, lineNumber, lineSource, columnNumber);
        }
        void recordErrorOrigin(String sourceName, int lineNumber,
                                 String lineSource, int columnNumber)
        {
            // XXX: for compatibility allow for now -1 to mean 0
            if (lineNumber == -1)
            {
                lineNumber = 0;
            }

            if (sourceName != null)
            {
                initSourceName(sourceName);
            }
            if (lineNumber != 0)
            {
                initLineNumber(lineNumber);
            }
            if (lineSource != null)
            {
                initLineSource(lineSource);
            }
            if (columnNumber != 0)
            {
                initColumnNumber(columnNumber);
            }
        }

        public void initSourceName(String sourceName)
        {
            if (sourceName == null) throw new ArgumentException();
            if (this.sourceName != null) throw new Exception();
            this.sourceName = sourceName;
        }
        public void initLineNumber(int lineNumber)
        {
            if (lineNumber <= 0) throw new ArgumentException((lineNumber.ToString()));
            if (this.lineNumber > 0) throw new Exception();
            this.lineNumber = lineNumber;
        }
        public void initLineSource(String lineSource)
        {
            if (lineSource == null) throw new ArgumentException();
            if (this.lineSource != null) throw new Exception();
            this.lineSource = lineSource;
        }
        public void initColumnNumber(int columnNumber)
        {
            if (columnNumber <= 0) throw new ArgumentException((columnNumber.ToString()));
            if (this.columnNumber > 0) throw new Exception();
            this.columnNumber = columnNumber;
        }
        private int columnNumber = 0;
        private String lineSource = null;
        private int lineNumber = 0;
        private String sourceName = null;
    }

}
